package cn.edu.sjtu.se.dslab.dao.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.aadl.AADLTemplate;
import cn.edu.sjtu.se.dslab.dao.IAADLTemplateDao;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("aadlTemplateDao")
public class AADLTemplateDaoImpl implements IAADLTemplateDao {
	private static final String URI = "uri";

	@Resource(name = "baseDao")
	private IBaseDao<AADLTemplate> baseDao;

	private static final String DELETED = "deleted";
	
	@Override
	public void save(AADLTemplate aadlTemplate) {
		// TODO Auto-generated method stub
		String temp = "aadl_template"+System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!checkUri(Const.URI_AADL_TEMPLATE + ":" + uri)) {
			temp = "aadl_template"+System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		aadlTemplate.setUri(Const.URI_AADL_TEMPLATE + ":" + uri);
		baseDao.save(aadlTemplate);
	}

	@Override
	public boolean delete(AADLTemplate aadlTemplate) {
		// TODO Auto-generated method stub
		aadlTemplate.setDeleted(true);
		update(aadlTemplate);
		return true;
	}

	@Override
	public boolean deleteById(long id) {
		// TODO Auto-generated method stub
		baseDao.delById(AADLTemplate.class, id);
		AADLTemplate aadlTemplate = queryById(id);
		aadlTemplate.setDeleted(true);
		update(aadlTemplate);
		return true;
	}

	@Override
	public boolean deleteByUri(String uri) {
		// TODO Auto-generated method stub
		AADLTemplate aadlTemplate = queryByUri(uri);
		delete(aadlTemplate);
		return true;
	}

	@Override
	public void update(AADLTemplate aadlTemplate) {
		// TODO Auto-generated method stub
		baseDao.update(aadlTemplate);
	}

	@Override
	public PageBean<AADLTemplate> queryByPageNum(int pageNo) throws DBException {
		// TODO Auto-generated method stub
		PageBean<AADLTemplate> bean = baseDao.queryByHqlForPage(pageNo,
				Const.PAGESIZE, "AADLTemplate");
		return bean;
	}

	@Override
	public PageBean<AADLTemplate> queryByPageNum(int pageNo, String name,
			String description,long groupid) throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AADLTemplate a where a.id>0 ");
		if (name != null && !name.equals("")) {
			hql.append(" and a.name like '%" + name.trim() + "%' ");
		}

		if (description != null && !description.equals("")) {
			hql.append(" and a.description like '%" + description.trim() + "%'");
		}
		if (groupid != 0) {
			hql.append(" and a.group.id =" + groupid + " ");
		}
		hql.append(" and "+DELETED + "=0");
		System.out.println("hql:"+hql);
		PageBean<AADLTemplate> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public AADLTemplate queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(AADLTemplate.class, id);
	}

	@Override
	public AADLTemplate queryByUri(String uri) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from AADLTemplate where ");
		hql.append(URI + "=");
		hql.append("'" + uri + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public boolean checkUri(String uri) {
		// TODO Auto-generated method stub
		AADLTemplate aadlTemplate = queryByUri(uri);
		if (aadlTemplate != null) {
			return false;
		} else {
			return true;
		}
	}

}
